<script setup>
import { ref, reactive} from 'vue'

import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import {toast} from "@/composables/utils.js";
const store = useStore()
const router = useRouter()
const form=reactive({
  username: 'admin',
  password: 'admin'
})
const rules=ref({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 10, message: '用户名长度在 3 到 10 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 5, max: 16, message: '密码长度在 6 到 16 个字符', trigger: 'blur' }
  ]
})
const formRef=ref(null)
const loading=ref(false)
const onSubmit=()=>{
  formRef.value.validate((valid) => {
    if (!valid) {
      return
    }
        loading.value=true
        store.dispatch('login',form).then((res) => {
          toast('登录成功')
          router.push('/')
        })
    // login(form.username, form.password).then(() => {
    //   toast('登录成功')
    //   router.push('/home')
    // })
        .finally(() => {
          loading.value=false
  })
  })
}

</script>

<template>
  <div>
    <el-row :gutter="20" class="login-container">
      <el-col :lg="16" :offset="0" :md="12" class="common bg">
        <div class="relative">
          <img src="@/assets/static/login-bg.jpg" alt="">
          <div class="absolute inset-0 flex items-center justify-center">
            <h2 class="text-3xl font-bold">商城后台系统</h2>
          </div>
        </div>
      </el-col>
      <el-col :lg="8" :offset="0" :md="12" class="common">
        <div class="login-form">
          <h2 class="font-bold text-3xl text-center mb-10">欢迎回来</h2>
          <el-form :model="form" ref="formRef" :rules="rules" class="w-full">
            <el-form-item label="" prop="username">
              <el-input v-model="form.username" placeholder="请输入用户名">
                <template #prefix >
                  <el-icon> <user /> </el-icon>
                </template> 
              </el-input>
            </el-form-item>
            <el-form-item label="" prop="password">
              <el-input v-model="form.password" placeholder="请输入密码" type="password" show-password>
                <template #prefix>
                  <el-icon> <lock /> </el-icon>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit" class="w-full rounded-tl-full rounded-br-full" :loading="loading">登录</el-button>
            </el-form-item>
          </el-form>          
        </div>
      </el-col>
    </el-row>    
  </div>
</template>

<style scoped>
  .login-container {
    @apply h-screen bg-gray-500;
  }
  .login-container .common{
    @apply flex justify-center items-center;
  }
  .login-container .common:nth-child(2){

    @apply bg-white;
  }
  .common .login-form{
    @apply flex items-center justify-center flex-col;
    width: 400px;
    height: 400px;
    border-radius: 10px;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0,0,0,0.3);
    padding: 20px;
    
  }
</style>